<template>
  <el-card style="margin-bottom: 10px"><div><h1>充值中心</h1></div></el-card>
  <el-card>
    <el-row>
      <el-col :span="20"><span style="color: red;font-weight: bold">我的余额</span>：￥{{userStore.userInfo.money}}  元</el-col>
      <el-col :span="1">
        <el-button type="danger" @click="addDialogShow=true"><el-icon><wallet /></el-icon>充值</el-button>
      </el-col>
    </el-row>
  </el-card>

  <!-- 充值开始 -->
  <el-dialog v-model="addDialogShow" title="充值" width="500">
    <el-form>
      <el-form-item label="金额：" label-width=20%>
        <el-input type="text" v-model="addMoney.sum" placeholder="请输入金额" autocomplete="off" style="width: 150px"/><span style="margin-left: 10px">元</span>
      </el-form-item>
      <div style="margin-left: 20px">
        <span class="contact" :class="{selectAddr :addMoney.sum== 50}" @click="addMoney.sum = 50">50元</span>
        <span class="contact" :class="{selectAddr :addMoney.sum== 100}" @click="addMoney.sum = 100">100元</span>
        <span class="contact" :class="{selectAddr :addMoney.sum== 1000}" @click="addMoney.sum = 1000">1000元</span>
        <span class="contact" >自定义</span>
      </div>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="addDialogShow = false">
          取消
        </el-button>
        <el-button type="primary" @click="recharge">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
  <!-- 充值结束 -->
</template>

<script setup>
import {useUserStore} from "@/stores/user.js";
import {ref} from "vue";
import userApi from "@/api/userApi.js";
import {ElMessage} from "element-plus";
const userStore = useUserStore()
const addDialogShow = ref(false)
const addMoney = ref({
  sum:null
})


//充值
function recharge(){
  userApi.recharge(addMoney.value)
      .then(resp => {
        if(resp.code == 10000){
          ElMessage.success(resp.msg);
          getInfo();
          addMoney.value={
            money: null
          }
          addDialogShow.value=false
        }else {
          ElMessage.error(resp.msg);
        }
      })
}
//获取用户信息
function getInfo(){
  userApi.getInfo()
      .then(resp => {
        if(resp.code == 10000){
          //将已登录的用户信息保存在pinia中
          userStore.updateUser(resp.data)
        }
      })
}
</script>

<style scoped>
.contact{
  font-weight: bold;
  font-size: 18px;
  padding-top: 20px;
  margin-right: 15px;
  display: inline-block;
  border: 2px solid #AAA;
  width: 80px;
  height: 50px;
  text-align: center;
  cursor: pointer;
}
.contact:hover{
  border: 2px solid #409EFF;
}
.selectAddr{
  border: 2px solid #409EFF;
}
</style>